/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include <string.h>

#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "usart.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for LED_Task */
osThreadId_t LED_TaskHandle;
const osThreadAttr_t LED_Task_attributes = {
  .name = "LED_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal1,
};
/* Definitions for UART_Task */
osThreadId_t UART_TaskHandle;
const osThreadAttr_t UART_Task_attributes = {
  .name = "UART_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal2,
};
/* Definitions for Button_Task */
osThreadId_t Button_TaskHandle;
const osThreadAttr_t Button_Task_attributes = {
  .name = "Button_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal3,
};
/* Definitions for xBtnSem */
osSemaphoreId_t xBtnSemHandle;
const osSemaphoreAttr_t xBtnSem_attributes = {
  .name = "xBtnSem"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void StartTask02(void *argument);
void StartTask03(void *argument);
void StartTask04(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* Create the semaphores(s) */
  /* creation of xBtnSem */
  xBtnSemHandle = osSemaphoreNew(1, 1, &xBtnSem_attributes);

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of LED_Task */
  LED_TaskHandle = osThreadNew(StartTask02, NULL, &LED_Task_attributes);

  /* creation of UART_Task */
  UART_TaskHandle = osThreadNew(StartTask03, NULL, &UART_Task_attributes);

  /* creation of Button_Task */
  Button_TaskHandle = osThreadNew(StartTask04, NULL, &Button_Task_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the LED_Task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void *argument)
{
  /* USER CODE BEGIN StartTask02 */
  /* Infinite loop */
  // for(;;)
  // {
  //   if (osSemaphoreAcquire(xBtnSemHandle, 0) == osOK)
  // {
  //   HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  //
  //
  //
  // }
  //   HAL_Delay(20);//消抖
  // }
  // for (;;)
  // {
  //   HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);   // 强制翻转
  //   osDelay(200);                             // 200 ms 快闪
  // }
  for (;;)
  {
    if (osSemaphoreAcquire(xBtnSemHandle, 0) == osOK)
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
    osDelay(20);   // 20 ms 扫描
  }


  /* USER CODE END StartTask02 */
}

/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the UART_Task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void StartTask03(void *argument)
{
  /* USER CODE BEGIN StartTask03 */
  /* Infinite loop */
  const char *hello = "Button pressed!\r\n";
  for (;;)
  {
    osSemaphoreAcquire(xBtnSemHandle, osWaitForever);//等待“遥控器”信号，即让灯亮
    HAL_UART_Transmit_DMA(&huart1, (uint8_t *)hello, strlen(hello));//不持续发送Button pressed!，因为若上一行一直等待信号，不会执行到这一行
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);

  }

  /* USER CODE END StartTask03 */
}

/* USER CODE BEGIN Header_StartTask04 */
/**
* @brief Function implementing the Button_Task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask04 */
void StartTask04(void *argument)
{
  /* USER CODE BEGIN StartTask04 */
  /* Infinite loop */
  // for (;;)
  // {
  //   if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)
  //   {
  //     osSemaphoreRelease(xBtnSemHandle);
  //     osDelay(200);   // 简单去抖
  //   }
  //   osDelay(10);
  // }
  uint8_t last = 1, now;
  for (;;)
  {
    now = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
    if (last == 1 && now == 0)      // 下降沿：松开瞬间
    {
      osSemaphoreRelease(xBtnSemHandle);
      osDelay(200);               // 简单去抖
    }
    last = now;
    osDelay(10);
  }
  /* USER CODE END StartTask04 */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* USER CODE BEGIN Header_StartLED_Task */
// void StartLED_Task(void *argument)
// {
//   for (;;)
//   {
//     if (osSemaphoreAcquire(xBtnSemHandle, 0) == osOK)
//       {
//       HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
//       /* 再闪一次，风格不同，肉眼可辨 */
//       HAL_Delay(50);
//       HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);  // 按键触发翻转
//       }
//     osDelay(20);     // 消抖
//   }
// }
// /* USER CODE END Header_StartLED_Task */
//
// /* USER CODE BEGIN Header_StartUART_Task */
// void StartUART_Task(void *argument)
// {
//   const char *hello = "Button pressed!\r\n";
//   for (;;)
//   {
//     osSemaphoreAcquire(xBtnSemHandle, osWaitForever);//等待“遥控器”信号，即让灯亮
//     HAL_UART_Transmit_DMA(&huart1, (uint8_t *)hello, strlen(hello));//不持续发送Button pressed!，因为若上一行一直等待信号，不会执行到这一行
//
//   }
// }
// /* USER CODE END Header_StartUART_Task */
//
// /* USER CODE BEGIN Header_StartButton_Task */
// void StartButton_Task(void *argument)
// {
//   for (;;)
//   {
//     if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
//     {
//       osSemaphoreRelease(xBtnSemHandle);
//       osDelay(200);   // 简单去抖
//     }
//     osDelay(10);
//   }
// }
/* USER CODE END Header_StartButton_Task */
/* USER CODE END Application */

